package com.zmkm.service.impl;

import com.zmkm.common.ServerResponse;
import com.zmkm.dao.Export_warehouseMapper;
import com.zmkm.dao.ProductMapper;
import com.zmkm.dao.Put_in_warehouseMapper;
import com.zmkm.dao.Warehouse_store_numberMapper;
import com.zmkm.entity.Export_warehouse;
import com.zmkm.entity.Product;
import com.zmkm.entity.Put_in_warehouse;
import com.zmkm.entity.Warehouse_store_number;
import com.zmkm.service.IWarehouseManageService;
import com.zmkm.util.StringOrInt;
import com.zmkm.vo.Put_in_warehouseRegister;
import com.zmkm.vo.export_warehouseRegister;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service("iWarehouseManageService")
public class WarehouseManageImpl implements IWarehouseManageService {
	@Autowired
	private ProductMapper productMapper;
	@Autowired
	private Warehouse_store_numberMapper wsnMapper;
	@Autowired
	private Put_in_warehouseMapper put_inMapper;
	@Autowired
	private Export_warehouseMapper export_wareMapper;

	@Override
	@SuppressWarnings("finally")
	@Transactional(rollbackFor = { RuntimeException.class })
	public ServerResponse put_inRegister(Put_in_warehouseRegister pwr) {
		boolean Vu = false;
		try {
			Product product = this.productMapper.selectByProductNameOrModel(pwr.getProduct_name(),
					pwr.getProduct_model());
			if ((product == null) || (product.getSpecification() == null)) {
				product = new Product();
				product.setNewProduct(pwr.getProduct_name(), pwr.getProduct_model(), pwr.getProduct_type(),
						pwr.getSpecification(), pwr.getUnit(), "", "");
				this.productMapper.insertSelective(product);
			}
			Put_in_warehouse put_in_warehouse = new Put_in_warehouse();
			Integer tota_money = Integer
					.valueOf(StringOrInt.toInt(pwr.getPut_in_price()).intValue() * pwr.getQuantity());
			put_in_warehouse.setNewPut_in_warehouse(pwr.getTime(), pwr.getWarehouse_address(), product.getId(),
					pwr.getPut_in_price(), Integer.valueOf(pwr.getQuantity()), StringOrInt.toString(tota_money),
					pwr.getSign_for_man(), pwr.getDelive_goods_man(), pwr.getCountry_of_origin(), pwr.getRemarks());
			if (this.put_inMapper.insertSelective(put_in_warehouse) <= 0) {
				Vu = false;
				throw new RuntimeException();
			}
			Warehouse_store_number wsn = this.wsnMapper.selectByProductIdOrAddressOrPrice(product.getId().intValue(),
					pwr.getWarehouse_address(), pwr.getPut_in_price());
			if ((wsn == null) || (wsn.getWarehouse_address() == null) || (wsn.getQuantity() == null)) {
				wsn = new Warehouse_store_number();
				wsn.setNewWarehouse_store_number(product.getId(), pwr.getPut_in_price(),
						Integer.valueOf(pwr.getQuantity()), pwr.getPut_in_price(), pwr.getWarehouse_address());
				if (this.wsnMapper.insertSelective(wsn) <= 0) {
					Vu = false;
					throw new RuntimeException();
				}
				Vu = true;
			} else {
				Integer quantity = wsn.getQuantity();
				int quantityInsert = pwr.getQuantity();
				Integer quantityNew = Integer.valueOf(quantity.intValue() + quantityInsert);
				wsn.setQuantity(quantityNew);
				if (this.wsnMapper.updateByPrimaryKeySelective(wsn) <= 0) {
					Vu = false;
					throw new RuntimeException();
				}
				Vu = true;
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			if (Vu) {
				return ServerResponse.createBySuccessMessage("提交成功");
			} else {
				return ServerResponse.createByErrorMessage("入库添加失败，请重新添加");
			}
		}
	}

	@Override
	@SuppressWarnings("finally")
	@Transactional(rollbackFor = { RuntimeException.class })
	public ServerResponse export_Register(export_warehouseRegister ewr) {
		boolean Vu = false;
		try {
			Product product = this.productMapper.selectByProductNameOrModel(ewr.getProduct_name(),
					ewr.getProduct_model());
			Export_warehouse ew = new Export_warehouse();
			Integer tota_money = Integer
					.valueOf(StringOrInt.toInt(ewr.getExport_price()).intValue() * ewr.getQuantity());
			ew.setNewExport_warehouse(ewr.getTime(), ewr.getWarehouse_address(), product.getId(), ewr.getExport_price(),
					Integer.valueOf(ewr.getQuantity()), StringOrInt.toString(tota_money), ewr.getExport_warehouse_man(),
					ewr.getFetch_warehouse_man(), ewr.getRemarks());
			if (this.export_wareMapper.insertSelective(ew) <= 0) {
				Vu = false;
				throw new RuntimeException();
			}
			List<Warehouse_store_number> wsnList = this.wsnMapper.selectByProductIdOrAddress(product.getId().intValue(),
					ewr.getWarehouse_address());
			int quantityReduce = ewr.getQuantity();
			for (Warehouse_store_number wsn : wsnList) {
				Integer quantity = wsn.getQuantity();
				if (quantity.intValue() > quantityReduce) {
					Integer quantityNew = Integer.valueOf(quantity.intValue() - quantityReduce);
					wsn.setQuantity(quantityNew);
					if (this.wsnMapper.updateByPrimaryKeySelective(wsn) <= 0) {
						Vu = false;
						throw new RuntimeException();
					}
					Vu = true;
				} else {
					quantityReduce -= quantity.intValue();
					wsn.setQuantity(Integer.valueOf(0));
					if (this.wsnMapper.updateByPrimaryKeySelective(wsn) <= 0) {
						Vu = false;
						throw new RuntimeException();
					}
					Vu = true;
					if (quantityReduce == 0) {
						break;
					}
				}
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			if (Vu) {
				return ServerResponse.createBySuccessMessage("提交成功");
			} else {
				return ServerResponse.createByErrorMessage("出库表添加失败，请重新添加");
			}
		}
	}

	@Override
	public List<export_warehouseRegister> exportWarehouseSelect(String type, String value) {
		export_warehouseRegister ewr = new export_warehouseRegister();
		String str;
		switch ((str = type).hashCode()) {
		case -480441736:
			if (str.equals("warehouse_address")) {
				ewr.setWarehouse_address(value);
			}
			break;
		case 3560141:
			if (!str.equals("time"))
				break;
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			try {
				ewr.setTime(df.parse(value));
			}
			// Misplaced declaration of an exception variable
			catch (ParseException e) {
				e.printStackTrace();
			}
			break;
		case 1014375387:
			if (str.equals("product_name")) {
				ewr.setProduct_name(value);
			}
			break;
		case 1130425756:
			if (str.equals("price_per_unit")) {
				ewr.setExport_price(value);
			}
			break;
		case 1341030681:
			if (str.equals("fetch_warehouse_man")) {
				ewr.setFetch_warehouse_man(value);
			}
			break;
		case 1380350937:
			if (str.equals("product_model")) {
				ewr.setProduct_model(value);
			}
			break;
		case 1972064403:
			if (!str.equals("export_warehouse_man")) {
				ewr.setExport_warehouse_man(value);
			}
			break;
		}
		return export_wareMapper.selectExportWarehouseR(ewr);
	}

	@Override
	public List<String> productSelect(String warehouse_address) {
		return this.export_wareMapper.selectExportProductByAddress(warehouse_address);
	}
}
